package com.mindtalk.screen.dialog;

import org.jivesoftware.smackx.muc.MultiUserChat;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.mindtalk.R;
import com.mindtalk.helper.ConnectionHelper;
import com.mindtalk.screen.Conferences;
import com.mindtalk.transferdata.MyChannelsData;

public class RoomSettings extends Dialog implements android.view.View.OnClickListener{

	/*
	 * Dialog yang muncul ketika akan mensetting room
	 * */
	
	/*
	 * 
	 * Parent Class
	 * */
	private Conferences conferences;
	
	/*
	 * MultiUserChat
	 * */
	MultiUserChat muc;
	
	/*
	 * Konstruktor
	 * */
	
	/*
	 * AlertDialog
	 * */
	AlertDialog alertDialog;
	public RoomSettings(Conferences conferences) {
		super(conferences);
		this.conferences = conferences;		
	}

	@Override
	public void onStart(){
		super.onStart();
		setContentView(R.layout.newroomdialog);
		//getWindow().setFlags(4, 4);
		setTitle("Create New Room Dialog");
		Button ok = (Button)findViewById(R.id.btn_l_newroomdialog_createroom);
		ok.setOnClickListener(this);			
		Button cancel = (Button)findViewById(R.id.btn_l_newroomdialog_cancel);
		cancel.setOnClickListener(this);
		alertDialog = new AlertDialog.Builder(getContext()).create();		
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				dismiss();
			}
		});
	}
	
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		/*
		 * Cancel Button
		 * */
		case R.id.btn_l_newroomdialog_cancel:
			dismiss();
			break;
		/*
		 * Ok Button
		 * */
		case R.id.btn_l_newroomdialog_createroom:{
			String host = ((EditText)findViewById(R.id.et_l_newroomdialog_host)).getText().toString();
			String roomname = ((EditText)findViewById(R.id.et_l_newroomdialog_room)).getText().toString();
			String nickname = ((EditText)findViewById(R.id.et_l_newroomdilaog_nickname)).getText().toString();
			String password = ((EditText)findViewById(R.id.et_l_newroomdialog_password)).getText().toString();	
			muc = ConnectionHelper.getMultiUserChat(roomname, nickname, host, password);			
			if(muc!=null){
				MyChannelsData data = new MyChannelsData("", roomname, 1, 0, muc);
				conferences.addAdapter(data);
				dismiss();
			}else{
				alertDialog.setTitle("Error");
				alertDialog.setMessage("Failed To Create Room");
				alertDialog.show();
				Log.e("createErr", "MuC NULL");
			}
			break;
		}
		default:
			break;
		}
					
	}
	
	
	

}
